package com.yulp.springboot2task.service;

import com.yulp.springboot2task.utils.DateUtil;
import net.greghaines.jesque.Job;
import net.greghaines.jesque.client.ClientPoolImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @date: 2019/11/7 17:53.
 * @author: yulipeng.
 */
@Service
public class TestJesQueImpl {

    @Autowired
    private ClientPoolImpl jesqueClientPool;

    public static final String PAYMET_RETRY_QUEUE_NAME = "payment-service-retry-queue";
    public static final String PAYMET_RETRY_ACTION_NAME = "PaymentConsumerRetryHandler";

    public static final String REFUND_RETRY_QUEUE_NAME = "refund-service-retry-queue";
    public static final String REFUND_RETRY_ACTION_NAME = "RefundMessageConsumerRetryHandler";

    //private static final int[] RETRY_DELAY = new int[] { 5, 30, 60, 300, 600, 3600, 14400 };
    private static final int[] RETRY_DELAY = new int[] { 1, 2, 2, 3, 3, 3, 4 };

    /**
     * 可继承RetryMessageSender
     * 该接口是否可以提炼出来，做成公共的方法类； 见：RetryMessageSender
     * @param topic
     * @param message
     * @param retries
     */
    public void a1(String topic, String message, int retries) {
        System.out.println("========topic: "+ topic+ "==message==:"+ message+"====retries:"+retries);
        if (retries >= 7) {
            System.out.println(DateUtil.currentDateLong()+ " 已超过次数7......");
            return;
        }
        final long future = System.currentTimeMillis() + (RETRY_DELAY[retries] * 1000);
        final Job job = new Job(PAYMET_RETRY_ACTION_NAME, topic, message, retries + 1, future+"");
        //创建执行一次定时任务
        jesqueClientPool.delayedEnqueue(PAYMET_RETRY_QUEUE_NAME, job, future);
        System.out.println(DateUtil.currentDateLong()+ " jesque入队.....");
    }

}
